﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmDanToc : Form
    {
        public frmDanToc()
        {
            InitializeComponent();
        }
        clsBULTT_DanToc cls;
        public void LoadDuLieu()
        {
            cls = new clsBULTT_DanToc();
            dgrDanToc.DataSource = cls.SelectAll();
            txtMaDT.Enabled = false;
            txtTenDT.Enabled = false;         
            btnThem.Text = "&Thêm";
            btnSua.Text = "&Sửa";
            //btnThem.Enabled = false;
            //btnSua.Enabled = false;
            //btnXoa.Enabled = false;
        }
        private void frmDanToc_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
            //btnThem.Enabled = false;
            //btnSua.Enabled = false;
            //btnXoa.Enabled = false;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {
                btnThem.Text = "&Lưu Thêm";
                txtMaDT.Text = "";
                txtTenDT.Text = "";
                txtMaDT.Enabled = true;
                txtTenDT.Enabled = true;
                txtMaDT.Focus();
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
            }
            else
            {
                if (txtMaDT.Text == "" || txtTenDT.Text == "" )
                {
                    MessageBox.Show("Chưa đủ thông tin !", "Lỗi dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    btnThem.Text = "&Thêm";
                    cls = new clsBULTT_DanToc();
                    cls.DanToc_ID = txtMaDT.Text;
                    cls.Name = txtTenDT.Text;
                    if (cls.TrungMa() == true)
                    {
                        MessageBox.Show("Trùng mã !", "Lỗi Trùng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        cls.Insert();
                        LoadDuLieu();
                        MessageBox.Show("Thêm thành công!");
                        btnXoa.Enabled = true;
                        btnSua.Enabled = true;
                    }
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            cls = new clsBULTT_DanToc();
            if (btnSua.Text =="&Sửa")
            {
                if (txtMaDT.Text == "")
                {
                    MessageBox.Show("Bạn chưa chọn dữ liệu để sửa!", "Lỗi Dữ Liêu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    btnSua.Text = "&Lưu Sửa";
                    txtMaDT.Enabled = false;
                    txtTenDT.Enabled = true;
                    btnThem.Enabled = false;
                    btnXoa.Enabled = false;
                    dgrDanToc.Enabled = false;
                }
            }
            else
            {                
                btnSua.Text = "&Sửa";
                cls.DanToc_ID = txtMaDT.Text;
                cls.Name = txtTenDT.Text;
                if (txtMaDT.Text == "" || txtTenDT.Text == "")
                {
                    MessageBox.Show("Không được rỗng.");
                }
                else
                {
                    cls.Update();
                    LoadDuLieu();
                    MessageBox.Show("Sửa thành công.");
                    btnThem.Enabled = true;
                    btnXoa.Enabled = true;
                    dgrDanToc.Enabled = true;
                }
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaDT.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa.","Lỗi Dữ Liêu",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult hoi = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này không?", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (hoi == DialogResult.Yes)
                {
                    cls = new clsBULTT_DanToc();
                    cls.DanToc_ID = txtMaDT.Text;
                    try
                    {
                        cls.Delete();
                        LoadDuLieu();
                        txtMaDT.Text = "";
                        txtTenDT.Text = "";
                        MessageBox.Show("Xóa thành công.");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Dữ liệu này không thể có được.");
                    }
                }
            }
        }

        private void dgrDanToc_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaDT.Text = dgrDanToc.CurrentRow.Cells[0].Value.ToString();
            txtTenDT.Text = dgrDanToc.CurrentRow.Cells[1].Value.ToString();
        }

        private void dgrDanToc_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            txtMaDT.Enabled = false;
            txtTenDT.Enabled = false;

            btnSua.Text = "&Sửa";
            btnThem.Text = "&Thêm";
        }
    }
}
